package exam.dfs;


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * HJ89
 * 24点运算
 *
 * 尝试dfs
 *
 * @author shenenlu 2021年05月04日 上午02:55:12
 */
public class Calculate24Dfs {


    private int[]  numberList;

    private boolean[]  visited;

    private Stack<Integer> temp;

    private List<List<Integer>> result;

    private boolean canCalculate;

    private static final int ADD = 0;
    private static final int SUBTRACT = 1;
    private static final int MULTIPLY = 2;
    private static final int DIVIDE = 3;


    public Calculate24Dfs(int[] numberList){
        this.numberList = numberList;
        this.canCalculate = false;
        this.result = new ArrayList<>();
        this.temp = new Stack<>();
        this.visited = new boolean[numberList.length];
    }



    public void dfs(int index, int result){
        if( canCalculate ){
            return;
        }
        if( index == 3 ){
            if( result == 24 ){
                canCalculate = true;
            }
            return;
        }

        for( int i = 0; i < 4; i++){
            for(int j = index; j < numberList.length; j++){
                switch (i) {
                    case ADD:
                        dfs(index + 1, result + numberList[i]);
                        break;
                    case SUBTRACT:
                        dfs(index + 1, result - numberList[i]);
                        break;
                    case MULTIPLY:
                        dfs(index + 1, result * numberList[i]);
                        break;
                    case DIVIDE:
                        dfs(index + 1, result / numberList[i]);
                        break;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] numberList = new int[]{1,2,3,4};
        Calculate24Dfs calculate24Dfs = new Calculate24Dfs(numberList);
//        calculate24Dfs.dfs(0, 0);
//        System.out.println(calculate24Dfs.canCalculate);

    }

}

